package org.example.java8.stream;

public class NullPointerException {
    public static void main(String[] args) {
        //会出现空指针异常
        //String insuranceName = getInsurance(new Person());

        String result = getInsuranceNameByDeepDoubts(new Person(null));
        System.out.println(result);
    }


    private static String getInsuranceNameByDeepDoubts(Person person) {
        if (null != person) {
            Car car = person.getCar();
            if (null != car) {
                Insurance insurance = car.getInsurance();
                if (null != insurance) {
                    return insurance.getName();
                }
            }
        }
        return "UNKNOWN";
    }

    private static String getInsuranceNameByMultiExit(Person person) {
        final String defaultValue = "UNKNOWN";
        if (null == person)
            return defaultValue;
        Car car = person.getCar();
        if (null == car)
            return defaultValue;
        Insurance insurance = car.getInsurance();
        if (null == insurance)
            return defaultValue;
        return insurance.getName();
    }

    private static String getInsurance(Person person) {
        return person.getCar().getInsurance().getName();
    }
}
